import 'dart:core';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:networking/src/http_method/nw_http_method.dart';
import 'package:networking/src/inject/inject.dart';

abstract class NWRequest<T> {
  // 当前请求是否为RefreshToken
  bool isRefreshToken = false;
  ProgressCallback? uploadProgress;
  ProgressCallback? downloadProgress;
  CancelToken? cancelToken;

  final timeout = const Duration(seconds: 150);

  Map<String, String> headers = {};
  HTTPMethod method = HTTPMethod.GET;
  Map<String, dynamic> query = {};
  Map<String, dynamic> body = {};
  File? file;
  ProgressCallback? onSendProgress;
  ProgressCallback? onReceiveProgress;
  List<NWInject>? injects;

  ResponseDecoder? responseDecoder;

  String baseUrl() {
    return "";
  }

  String path() {
    return "";
  }

  addHeaders(String key, String val) {
    headers[key] = val;
  }

  /*
  设置domain的query
  如果是POST，此参也会附加到Path后面
  */
  addQuery(String key, dynamic val) {
    query[key] = val;
  }

  String? queryToString() {
    if (query.isEmpty) return null;
    String string = "?";
    for (var key in query.keys) {
      string += "$key=${query[key]}&";
    }
    string = string.substring(0, string.lastIndexOf("&"));
    string = Uri.encodeComponent(string);
    return string;
  }

  /*
  设置Body
  任何请求如果设置了此属性，都会附加到请求的body中
  */
  addBody(String key, dynamic val) {
    body[key] = val;
  }

  // 设置拦截器
  List<Interceptor>? interceptors() {
    return null;
  }

  T? parseData(dynamic data) {
    return null;
  }
}
